﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QldtSdh.BLL
{
    using DAL.Models;

    class BizAppConfig
    {
        private static string _GetValueOfKey(string key, string defaultValue, CEMS4UG_DHKHContext db = null)
        {
            if (db == null)
                using (var db2 = CEMS4UG_DHKHContext.GetContext())
                    return _GetValueOfKey(key, defaultValue, db2);
            else
            {
                var oKeyValueItem = db.KeyValueDbSet.Where(e => e.Key == key).FirstOrDefault();
                if (oKeyValueItem == null)
                {
                    oKeyValueItem = new KeyValueConfig() { Key = key, Value = defaultValue };
                    db.KeyValueDbSet.Add(oKeyValueItem);
                    db.SaveChanges();
                    return oKeyValueItem.Value;
                }
                else
                    return oKeyValueItem.Value;
            }
        }

        private static void _SetValueOfKey(string key, string value, CEMS4UG_DHKHContext db = null)
        {
            if (db == null)
                using (var db2 = CEMS4UG_DHKHContext.GetContext())
                    _SetValueOfKey(key, value, db2);
            else
            {
                var oKeyValueItem = db.KeyValueDbSet.Where(e => e.Key == key).FirstOrDefault();
                if (oKeyValueItem == null)
                {
                    oKeyValueItem = new KeyValueConfig() { Key = key, Value = value };
                    db.KeyValueDbSet.Add(oKeyValueItem);
                    db.SaveChanges();
                }
                else
                {
                    oKeyValueItem.Value = value;
                    db.SaveChanges();
                }
            }
        }

        #region Trưởng phòng
        private const string KEY_TRUONG_PHONG = "TRUONG_PHONG";
        internal static string GetTruongPhong(CEMS4UG_DHKHContext db = null)
        {
            return _GetValueOfKey(KEY_TRUONG_PHONG, "", db);
        }

        internal static void SetTruongPhong(string value, CEMS4UG_DHKHContext db = null)
        {
            _SetValueOfKey(KEY_TRUONG_PHONG, value, db);
        }
        #endregion

        #region Phó trưởng phòng
        private const string KEY_PHO_TRUONG_PHONG = "PHO_TRUONG_PHONG";
        internal static string GetPhoTruongPhong(CEMS4UG_DHKHContext db = null)
        {
            return _GetValueOfKey(KEY_PHO_TRUONG_PHONG, "", db);
        }

        internal static void SetPhoTruongPhong(string value, CEMS4UG_DHKHContext db = null)
        {
            _SetValueOfKey(KEY_PHO_TRUONG_PHONG, value, db);
        }
        #endregion

        #region Địa chỉ E-mail của phòng đào tạo
        private const string KEY_EMAIL = "EMAIL_UID";
        internal static string GetEmail(CEMS4UG_DHKHContext db = null)
        {
            return _GetValueOfKey(KEY_EMAIL, "", db);
        }

        internal static void SetEmail(string value, CEMS4UG_DHKHContext db = null)
        {
            _SetValueOfKey(KEY_EMAIL, value, db);
        }
        #endregion

        #region Mật khẩu E-mail của phòng đào tạo
        private const string KEY_EMAIL_PASSWORD = "EMAIL_PASSWORD";
        internal static string GetEmailPassword(CEMS4UG_DHKHContext db = null)
        {
            return _GetValueOfKey(KEY_EMAIL_PASSWORD, "", db);
        }

        internal static void SetEmailPassword(string value, CEMS4UG_DHKHContext db = null)
        {
            _SetValueOfKey(KEY_EMAIL_PASSWORD, value, db);
        }
        #endregion

        const string KEY_EMAIL_SMTP_SERVER_ADDRESS = "EMAIL_SMTP_SERVER";
        const string KEY_EMAIL_SMTP_PORT = "EMAIL_SMTP_PORT";
        const string KEY_EMAIL_SSL = "EMAIL_USE_SSL";

        #region SMTP Server Address
        internal static string GetEmailSmtpServerAddress(CEMS4UG_DHKHContext db = null)
        {
            return _GetValueOfKey(KEY_EMAIL_SMTP_SERVER_ADDRESS, "smtp.gmail.com", db);
        }

        internal static void SetEmailSmtpServerAddress(string value, CEMS4UG_DHKHContext db = null)
        {
            _SetValueOfKey(KEY_EMAIL_SMTP_SERVER_ADDRESS, value, db);
        }
        #endregion

        #region SMTP Port
        internal static int GetEmailSmtpPort(CEMS4UG_DHKHContext db = null)
        {
            return Convert.ToInt32(_GetValueOfKey(KEY_EMAIL_SMTP_PORT, "587"));
        }

        internal static void SetEmailSmtpPort(int value, CEMS4UG_DHKHContext db = null)
        {
            _SetValueOfKey(KEY_EMAIL_SMTP_PORT, value.ToString(), db);
        }
        #endregion

        #region Use SSL for email sending
        internal static bool GetEmailUseSSL(CEMS4UG_DHKHContext db = null)
        {
            return _GetValueOfKey(KEY_EMAIL_SSL, "true", db).ToLower() == "true";
        }

        internal static void SetEmailUseSSL(bool value, CEMS4UG_DHKHContext db = null)
        {
            _SetValueOfKey(KEY_EMAIL_SSL, value.ToString(), db);
        }
        #endregion

        internal static void SaveAppConfig(string truongPhong, string phoTruongPhong, string email, string emailPassword,
            string smtpServer, int smtpPort, bool useSSL)
        {
            using (var db = DAL.Models.CEMS4UG_DHKHContext.GetContext())
            {
                SetTruongPhong(truongPhong, db);
                SetPhoTruongPhong(phoTruongPhong, db);
                SetEmail(email, db);
                SetEmailPassword(emailPassword, db);
                SetEmailSmtpServerAddress(smtpServer, db);
                SetEmailSmtpPort(smtpPort, db);
                SetEmailUseSSL(useSSL, db);
            }
        }
    }
}
